package com.home.job.zh1.job2;


import java.text.DecimalFormat;

public class Probability {
    /*
     * 区域划分为 边长为10的正方形， 在直角坐标系中，区域为 x -> [-5, 5], y -> [-5, 5]
     */
    public static void main(String[] args) {
        DecimalFormat df = new DecimalFormat("0.00");
        for (int j = 0; j < 10; j++) {
            int count = 0;
            for (int i = 0; i < 100000; i++) {
                Point p = getRandomPoint();
                if (hasOne(p) || hasThree(p)) {
                    count++;
                }
            }
            System.out.println("概率为: " + df.format((float)count / 100000 * 100) + " %");
        }
    }

    // 判断在 1 区域
    public static boolean hasOne(Point p) {
        return p.x < 0;
    }

    // 判断在 1 区域
    public static boolean hasThree(Point p) {
        return p.x > 0 && p.y > 0 && p.getThree();
    }

    // 生成随机点
    public static Point getRandomPoint() {
        Point p = new Point();
        p.x = Math.random() * 10 - 5;
        p.y = Math.random() * 10 - 5;
        return p;
    }

    static class Point {
      public double x;
      public double y;

      public boolean getThree() {
          return this.y < (-1 * x - 5);
      }
    }
}

